//
//  SongerViewController.m
//  CapriceMusic
//
//  Created by dllo on 16/1/11.
//  Copyright © 2016年 MYX. All rights reserved.
//

#import "SongerViewController.h"
#import "SearchTableViewCell.h"
#import "SongerModel.h"
@interface SongerViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, copy) NSString *url;

@property (nonatomic, strong) NSMutableArray *dataSource;

@end

@implementation SongerViewController
/**
 *  http://mapi.yinyuetai.com/artist/show.json?D-A=0&offset=0&artistId=15466&size=20
 */
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(backDidPress:)];
    self.title = self.actor.name;
    self.url = @"http://mapi.yinyuetai.com/artist/show.json?D-A=0";
    self.dataSource = [NSMutableArray arrayWithCapacity:0];
    [self setUpTableView];
    [self getDataWithOffset:0];
    
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(receiveNotification:) name:@"collect" object:nil];
    // Do any additional setup after loading the view.
}

- (void)backDidPress:(UIBarButtonItem *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;

}
- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];

    [[NSNotificationCenter defaultCenter]removeObserver:self];

}
#pragma mark - tableView
- (void)setUpTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height - 64) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[SearchTableViewCell class] forCellReuseIdentifier:@"SearchTableViewCellIdentifier"];
    [self.view addSubview:self.tableView];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SearchTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SearchTableViewCellIdentifier"];
    if (self.dataSource.count > indexPath.row) {
        cell.searchMvModel = self.dataSource[indexPath.row];
    }
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 130)];
    
    UIImageView *userImage = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.width / 2 - 45, 10, 90, 90)];
    userImage.layer.cornerRadius = 45;
    userImage.contentMode = UIViewContentModeScaleAspectFill;
    userImage.clipsToBounds = YES;
    [userImage sd_setImageWithURL:[NSURL URLWithString:self.actor.smallAvatar]];
    [myView addSubview:userImage];
    
    UIImageView *star = [[UIImageView alloc] initWithFrame:CGRectMake(myView.width / 2 - 100, userImage.bottom + 10, 10, 10)];
    star.image = [UIImage imageNamed:@"star"];
    [myView addSubview:star];
    
    UIImageView *heart = [[UIImageView alloc] initWithFrame:CGRectMake(myView.width / 2 + 15, star.origin.y, 10, 10)];
    heart.image = [UIImage imageNamed:@"heart"];
    [myView addSubview:heart];
    
    UILabel *videoCount = [[UILabel alloc] initWithFrame:CGRectMake(star.origin.x + 10, star.origin.y, 70, 10)];
    videoCount.text = [NSString stringWithFormat:@"%@  首MV", self.actor.videoCount.stringValue];
    videoCount.font = [UIFont systemFontOfSize:12];
    [myView addSubview:videoCount];
    
    UILabel *subCount = [[UILabel alloc] initWithFrame:CGRectMake(heart.origin.x + 15, heart.origin.y, 90, 10)];
    subCount.text = [NSString stringWithFormat:@"%@ 个人订阅", self.actor.subCount.stringValue];
    subCount.font = [UIFont systemFontOfSize:12];
    [myView addSubview:subCount];
    return myView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 130;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 90;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    DetailViewController *detailVC = [[DetailViewController alloc] init];
    SongerModel *mvpageModel = self.dataSource[indexPath.row];
    detailVC.appendingId = mvpageModel.ids;
    detailVC.type = @"VIDEO";
    [self.navigationController pushViewController:detailVC animated:YES];
}

#pragma mark - 网络请求
- (void)getDataWithOffset:(NSInteger)offset {

    [JEHNetWorkTool getWithUrl:self.url parameter:@{@"offset":[NSString stringWithFormat:@"%ld", (long)offset], @"artistId":self.artistid, @"size":@"20"} httpHeader:@{@"App-Id":@"10201029",@"Device-Id":@"5fc617ae1220d0969b23eaab4c8f02b5",@"Device-V":@"QW5kcm9pZF80LjQuNF8xMDgwKjE5MjBfMTEwMDA5MDAw"} responseType:ResponseTypeJSON success:^(id result) {
        for (NSDictionary *dic in result[@"videos"]) {
            SongerModel *songerModel = [[SongerModel alloc] initWithDictionary:dic];
            [self.dataSource addObject:songerModel];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
    } fail:^(NSError *error) {
        NSLog(@"%@", error);
    }];
}
#pragma mark - 通知
- (void)receiveNotification:(NSNotification *)messages {
    NSString *str = messages.object;
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:str preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }]];
    [self presentViewController:alert animated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
